package com.zeus.controller;

import com.zeus.common.enums.ZeusAlarmLevelEnum;
import com.zeus.core.open.api.ZeusAlarm;
import com.zeus.core.open.api.annotation.ZeusAlarmAop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Objects;

/**
 * 测试API
 *
 * @author ShiQing_Chen  2022/3/1 2:22 下午
 */
@Slf4j
@RestController
public class LogSampleController {

    @Resource
    private Environment environment;


    @RequestMapping(value = "/debug-log", method = RequestMethod.GET)
    public String debug() {
        log.debug("### this is dubug error >> curTime = {}", getCurDateString());
        return "success";
    }

    // 再依次生成info、warn、error级别的请求

    @RequestMapping(value = "/info-log", method = RequestMethod.GET)
    public String info() {
        log.info("### this is info error >> curTime = {}", getCurDateString());
        return "success";
    }

    @RequestMapping(value = "/warn-log", method = RequestMethod.GET)
    public String warn() {
        log.warn("### this is warn error >> curTime = {}", getCurDateString());
        return "success";
    }

    @RequestMapping(value = "/error-log", method = RequestMethod.GET)
    public String error() {
        log.error("### this is error error >> curTime = {}", getCurDateString());
        return "success";
    }

    @ZeusAlarmAop(level = ZeusAlarmLevelEnum.P1, message = "请求出现异常请及时处理", solution = "让客服跟进")
    @RequestMapping(value = "/aop", method = RequestMethod.GET)
    public String test(@RequestParam String id) {
        if(Objects.equals(id, "1")) {
            throw new IllegalArgumentException("this is Exception");
        }
        return "success";
    }

    @RequestMapping(value = "/method", method = RequestMethod.GET)
    public String method() {
        ZeusAlarm.p1("this is method error", "让客服跟进");
        return "success";
    }

    private String getCurDateString() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
    }

}
